#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>
#include <chrono>


#define CRLF "\r\n"
QStateMachine g_machine;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    OnInitDialog();
}

MainWindow::~MainWindow()
{
    m_connect = false;
    m_exit = true;
    m_cv.notify_all();
    m_state = ThreadState::IDLE_STATE;
    if(m_work_thread.joinable())m_work_thread.join();
    if(m_rtsp)delete  m_rtsp;
    delete ui;
}

void MainWindow::OnInitDialog()
{
    ui->lineEdit_rtsp->setPlaceholderText(tr("Please rtsp addr."));
    ui->lineEdit_rtsp->setClearButtonEnabled(true);
    ui->lineEdit_rtsp->setText("rtsp://admin:jaya_2025@192.168.1.33:554/ch1/main/av_stream");
    if(nullptr == m_rtsp)
        m_rtsp = new RtspCtrl(this);
    Q_ASSERT(m_rtsp);
    InitUI();
    InitSlot();
    InitThread();
}

void MainWindow::InitSlot()
{
    connect(m_rtsp,&RtspCtrl::signal_init_result,this,[this](bool flag){
        QMessageBox::information(this,"info",QString("init result %1").arg(flag));
        m_connect = flag;
    });

    connect(m_rtsp,&RtspCtrl::signal_update_image,this,[this](const QImage&img){
        ui->label_pic->setScaledContents(true);//must be first than setpixmap other crash.
        ui->label_pic->setPixmap(QPixmap::fromImage(img));

    });
    connect(ui->pushButton_open,&QPushButton::clicked,this,[this]{
        m_rtsp->InitRtsp(ui->lineEdit_rtsp->text().trimmed(),ui->label_pic);
    });

    connect(ui->pushButton_play,&QPushButton::clicked,this,[this]{
        if(m_rtsp)
            m_rtsp->PlayRtsp();
    });
    connect(ui->pushButton_stop,&QPushButton::clicked,this,[this]{
        if(m_rtsp)
            m_rtsp->StopRtsp();
    });
    connect(ui->pushButton_grab,&QPushButton::clicked,this,[this]{
        if(m_rtsp)
            m_rtsp->GrabPic();
    });
    connect(ui->pushButton_quit,&QPushButton::clicked,this,&QCoreApplication::quit);
    connect(ui->pushButton_name,&QPushButton::clicked,this,[this]{
        if(m_rtsp)
            m_rtsp->GrapPic("fuck.jpg");
    });
    connect(ui->pushButton_thread,&QPushButton::clicked,this,[this]{
        auto content = ui->pushButton_thread->text().trimmed();
        if("on" == ui->pushButton_thread->text())
            m_state = ThreadState::CACULATE_STATE;
        else if("off" == ui->pushButton_thread->text())
            m_state = ThreadState::IDLE_STATE;
    });
}

void MainWindow::InitUI()
{
    if(auto btn = ui->pushButton_thread;btn)
    {
        auto on = new QState();
        auto off = new QState();

        on->assignProperty(btn,"text","on");
        off->assignProperty(btn,"text","off");
        on->addTransition(btn,&QPushButton::clicked,off);
        off->addTransition(btn,&QPushButton::clicked,on);

        g_machine.addState(on);
        g_machine.addState(off);
        g_machine.setInitialState(on);
        g_machine.start();
    }
}

void MainWindow::InitThread()
{
    m_work_thread = std::thread([this]{
        std::unique_lock<std::mutex>loc(m_cv_mutex);
        while(!m_exit)
        {
            switch(m_state)
            {
            case ThreadState::IDLE_STATE:
            {
                m_cv.wait_for(loc,std::chrono::milliseconds(200),[this]{return m_exit;});
                continue;
            }
                break;
            case ThreadState::CACULATE_STATE:
            {
                if(m_rtsp && m_connect)
                {
                    static int count{0};
                    m_rtsp->GrapPic(QString("%1.jpg").arg(count++));
                    m_cv.wait_for(loc,std::chrono::milliseconds(200),[this]{return m_exit;});
                }
            }
                break;
            default:
                break;
            }//switch
        }
    });
}

